package com.chisheng.caa.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chisheng.caa.config.CaaProperties;
import com.chisheng.caa.mapper.CourseMapper;
import com.chisheng.caa.pojo.dto.CourseDTO;
import com.chisheng.caa.pojo.dto.CourseQuery;
import com.chisheng.caa.pojo.entity.Course;
import com.chisheng.caa.pojo.response.PageDTO;
import com.chisheng.caa.service.CourseService;
import com.chisheng.caa.util.MathUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    private CaaProperties properties;

    @Resource
    private CourseMapper courseMapper;

    @Override
    public List<Integer> getGradeYearList() {
        // 已存在的年级年份
        List<Integer> l1 = courseMapper.selectList(null)
                .stream().map(Course::getEnrollmentYear)
                .distinct().collect(Collectors.toList());

        // 配置推荐的年级年份
        int yearNow = Calendar.getInstance().get(Calendar.YEAR);
        List<Integer> l2 = MathUtil.range(
                yearNow - properties.getExporting().getGradeYearLowerLimit(),
                yearNow + properties.getExporting().getGradeYearUpperLimit());

        List<Integer> yearList = MathUtil.distinct(MathUtil.union(l1, l2));
        Collections.sort(yearList);
        return yearList;
    }

    @Override
    public PageDTO<CourseDTO> getPage(Page<Course> page, CourseQuery query) {
        LambdaQueryWrapper<Course> wrapper = null;
        if (query != null) {
            wrapper = new LambdaQueryWrapper<>();
            String courseNature = query.getCourseNature();
            if (StrUtil.isNotEmpty(courseNature)) {
                wrapper.like(Course::getCourseNature, courseNature);
            }
            String courseCode = query.getCourseCode();
            if (StrUtil.isNotEmpty(courseCode)) {
                wrapper.eq(Course::getCourseCode, courseCode);
            }
            String courseName = query.getCourseName();
            if (StrUtil.isNotEmpty(courseName)) {
                wrapper.like(Course::getCourseName, courseName);
            }
            Integer studyingTerm = query.getStudyingTerm();
            if (ObjectUtil.isNotNull(studyingTerm)) {
                wrapper.eq(Course::getStudyingTerm, studyingTerm);
            }
            String openingCollege = query.getOpeningCollege();
            if (StrUtil.isNotEmpty(openingCollege)) {
                wrapper.like(Course::getOpeningCollege, openingCollege);
            }
        }

        Page<Course> coursePage = courseMapper.selectPage(page, wrapper);
        return PageDTO.getInstance(coursePage, CourseDTO.class);
    }

}
